using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using UnityEngine;
using UnityEngine.UI;
using System;
using DG.Tweening;

public class Tools : MonoBehaviour
{
    public static async Task<JObject> LoadJsonAsync(string resourcePath)
    {
        // 异步加载资源
        ResourceRequest resourceRequest = Resources.LoadAsync<TextAsset>(resourcePath);

        // 等待资源加载完成
        while (!resourceRequest.isDone)
        {
            await Task.Yield();  // 暂停当前异步任务，等待下一帧
        }

        // 加载完成后返回解析后的JObject
        if (resourceRequest.asset != null)
        {
            JObject config = JObject.Parse(resourceRequest.asset.ToString());
            return config;
        }
        else
        {
            Debug.LogError("Failed to load resource.");
            return null;  // 如果加载失败，返回null
        }
    }

    public static List<T> ShuffleList<T>(List<T> list)
    {
        List<T> shuffledList = new List<T>(list);
        int n = shuffledList.Count;
        System.Random rng = new System.Random();

        while (n > 1)
        {
            n--;
            int k = rng.Next(n + 1);
            T value = shuffledList[k];
            shuffledList[k] = shuffledList[n];
            shuffledList[n] = value;
        }

        return shuffledList;
    }

    public static GameObject TipToastObject;
    public static GameObject TipsContainer;
    public static void ShowTipToast(string message)
    {
        if (TipsContainer == null)
        {
            return;
        }

        if (TipToastObject != null)
        {
            GameObject clonedObject = Instantiate(TipToastObject);
            clonedObject.GetComponent<TipToast>().SetMsg(message);
            clonedObject.transform.SetParent(TipsContainer.transform, false);
            RectTransform rectTransform = clonedObject.GetComponent<RectTransform>();

            if (rectTransform != null)
            {
                rectTransform.localScale = Vector3.one;
                rectTransform.localPosition = Vector3.zero;
            }

            Sequence sequence = DOTween.Sequence();
            sequence.Append(rectTransform.DOMoveY(rectTransform.position.y + 130f, 1f).SetEase(Ease.OutQuad));
            sequence.Append(rectTransform.GetComponent<CanvasGroup>().DOFade(0f, 0.5f).SetEase(Ease.InQuad));
            sequence.OnKill(() =>
            {
                Destroy(clonedObject);
            });
        }
    }

    public static void HideAdsPanel()
    {

    }
    public static void ShowAdsPanel()
    {

    }

    public static void Log(string key, string value = "")
    {
        Debug.Log($"{key}====> {value}");
    }

    public static void AFLogEvent(string eventName, Dictionary<string, string> stringValues)
    {
        // AppsFlyer.sendEvent(eventName, stringValues);
    }

        // 生成6位数随机Uid的方法
    public static int GenerateRandomUid()
    {
        System.Random random = new System.Random();
        int uid = random.Next(100000, 1000000);
        return uid;
    }
}
